<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="~@/assets/imgs/logo1.png" alt="" />
        <span>shop.system.web</span>
      </div>
      <el-button type="primary" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '210px'">
        <div class="toggle-button" @click="toggleClick">|||</div>

        <!-- 侧边栏菜单区域 -->
        <el-menu
          unique-opened
          class="el-menu-vertical-demo"
          background-color="#0b3861"
          text-color="#F2F2F2"
          active-text-color="#FFFF00"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in memuList"
            :key="item.id"
          >
            <!-- 一级菜单的模板区域 -->
            <template #title>
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
            >
              <template #title>
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 右侧主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { getMenuList } from "@/network/menuList";

export default {
  name: "Home",
  data() {
    return {
      memuList: [],
      iconsObj: {
        125: "el-icon-user-solid",
        103: "el-icon-key",
        101: "el-icon-shopping-cart-full",
        102: "el-icon-document",
        145: "el-icon-potato-strips",
      },
      isCollapse: false,
    };
  },
  methods: {
    logout() {
      //清除缓存
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    //菜单的折叠与张开
    toggleClick() {
      this.isCollapse = !this.isCollapse;
    },

    /** 网络方法，获取所有菜单栏 */
    getMenus() {
      getMenuList().then((res) => {
        if (res.meta.status != 200)
          return this.$message.error("获取下拉菜单失败！" + res.meta.msg);
        this.memuList = res.data;
      });
    },
  },

  //生命周期函数
  created() {
    this.getMenus();
  },
};
</script>

<style lang="less" scoped>
.el-header {
  background-color: #084b8a;
  display: flex;
  justify-content: space-between;
  padding-left: 3px;
  align-items: center;
  color: #ffffff;
  font-size: 23px;
  height: 90px !important;

  > div {
    display: flex;
    align-items: center;
    img {
      height: 60px;
      width: 60px;
      border-radius: 100%;
      box-shadow: 0 0 10px #ffffff;
      padding: 3px;
      margin-left: 20px;
    }
    span {
      margin-left: 20px;
    }
  }
}

.home-container {
  height: 100%;
}
.el-aside {
  background-color: #0b3861;
  .el-menu {
    border-right: 0;
  }
}
.el-container {
  background-color: #f2f2f2;
}

.el-submenu {
  span {
    color: #f2f2f2;
    font-size: 15px;
    margin-left: 10px;
  }
  i {
    font-size: 20px;
  }
}

.toggle-button {
  background-color: #086a87;
  font-size: 20px;
  line-height: 25px;
  color: #ffffff;
  text-align: center;

  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>